home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 37 / Amiga Format CD37 (1999-02-16)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-03].iso / -screenplay- / shareware / invasionforce / source / utils.c < prev   
C/C++ Source or Header  |  1999-01-09  |  3KB  |  131 lines

  1. // Utils.C - general purpose utility functions
  2.  
  3. #include <dos.h>
  4. #include <proto/dos.h>
  5.  
  6. #include <exec/memory.h>
  7. #include <proto/exec.h>
  8.  
  9. #include <string.h>
  10. #include "Utils_protos.h"
  11.  
  12. // extern long Console;
  13.  
  14.    // Print to the console: used mainly for error reports & debugging.
  15. void Print(string)
  16. char *string;
  17. {
  18.    ULONG con = Output();
  19.  
  20.    if (con)
  21.       Write(con,string,strlen(string));
  22. }
  23.  
  24. short LeftButton()  // returns TRUE if left mouse button is down
  25. {
  26.    char *pra = (char *)0xBFE001;  // CIA-A port register A
  27.  
  28.    if ( (*pra & 0x40)==0 )
  29.       return TRUE;
  30.    else
  31.       return FALSE;
  32. }
  33.  
  34.  
  35. void wait_for_click()  // Wait for a click of the left mouse button.
  36. {
  37.    while ( !LeftButton() )  // wait for click
  38.       ;
  39. }
  40.  
  41.  
  42. ULONG FLength(filename)
  43. char *filename;
  44. {
  45.    // FLength() returns the length of the file "filename".  If the file
  46.    // cannot be found, it will return a length of -1.
  47.  
  48.    struct FileLock *flock;
  49.    struct FileInfoBlock *info;
  50.    ULONG result;
  51.  
  52.    flock = (struct FileLock *)Lock(filename, ACCESS_READ);  // get a lock
  53.    if (!flock)
  54.       return -1L;  // file not found
  55.  
  56.    // NOTE: RAM for the FileInfoBlock must be allocated dynamically to make
  57.    //       sure it is on an even boundary.
  58.  
  59.    info = (struct FileInfoBlock *)AllocMem(sizeof(*info),MEMF_CLEAR);
  60.    if (!info)
  61.       return -1L;  // some kind of memory problem
  62.    Examine((BPTR)flock, info);
  63.    result = info->fib_Size;      // get the file size
  64.    FreeMem(info,(long)sizeof(*info));  // free the ram
  65.    UnLock((BPTR)flock);  // release the lock
  66.    return result;
  67. }
  68.  
  69.  
  70. void FJoin(Source1,Source2,Dest)
  71. char *Source1, *Source2, *Dest;
  72. {
  73.    char *Buffer;
  74.    long File,
  75.         Size1 = FLength(Source1),
  76.         Size2 = FLength(Source2);
  77.  
  78.    if (Size1<0 || Size2<0)
  79.       return;
  80.  
  81.    Buffer = (char *)AllocMem(Size1+Size2,0L);
  82.    if (Buffer == NULL)
  83.       return;
  84.  
  85.    File = Open(Source1,MODE_OLDFILE);
  86.    Read(File,Buffer,Size1);
  87.    Close(File);
  88.  
  89.    File = Open(Source2,MODE_OLDFILE);
  90.    Read(File,Buffer+Size1,Size2);
  91.    Close(File);
  92.  
  93.    File = Open(Dest,MODE_NEWFILE);
  94.    Write(File,Buffer,Size1+Size2);
  95.    Close(File);
  96.  
  97.    FreeMem(Buffer,Size1+Size2);
  98. }
  99.  
  100.  
  101. void FCopy(Source,Dest)
  102. char *Source, *Dest;
  103. {
  104.    char *Buffer;
  105.    long File;
  106.    long Size = FLength(Source);
  107.  
  108.    if (Size<1)
  109.       return;
  110.  
  111.    Buffer = (char *)AllocMem(Size,0L);
  112.    if (Buffer == NULL)
  113.       return;
  114.  
  115.    File = Open(Source,MODE_OLDFILE);
  116.    if (File == NULL)
  117.       return;
  118.    Read(File,Buffer,Size);
  119.    Close(File);
  120.  
  121.    File = Open(Dest,MODE_NEWFILE);
  122.    if (File == NULL)
  123.       return;
  124.    Write(File,Buffer,Size);
  125.    Close(File);
  126.  
  127.    FreeMem(Buffer,Size);
  128. }
  129.  
  130. // end of listing
  131.